home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Commands / Format Table.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  15.3 KB  |  446 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. // *************** GLOBAL VARS  *****************
  4.  
  5. var helpDoc = MM.HELP_cmdFormatTable;
  6. var DEFAULT_tableStyle = 7;
  7. //these constants used in tableFormats.js file when defining new table formats
  8. var NONE=0,LEFT=1,CENTER=2,RIGHT=3,BOLD=1,ITALIC=2,BOLD_ITALIC=3;
  9. var ORIGINAL_PREVIEW_TABLE = getPreviewTable().outerHTML;
  10.  
  11. //******************* API **********************
  12.  
  13. function commandButtons(){
  14.   var tableObj = findTable();
  15.   var Buttons=new Array(BTN_OK,        "applyFormatToSelectedTable();window.close();",
  16.                         BTN_Apply,     "applyFormatToSelectedTable()",
  17.                         BTN_Cancel,    "window.close()",
  18.                                                 BTN_Help,      "displayHelp()");
  19.   if (isUnrecognizedTable(tableObj) || hasCaption(tableObj))
  20.     Buttons=new Array(BTN_OK,"window.close()",
  21.                       BTN_Cancel,"window.close()");
  22.  
  23.   return Buttons;
  24. }
  25.  
  26. function canAcceptCommand(){
  27.   if (findTable())
  28.     return true;
  29.   else
  30.     return false;
  31. }
  32.  
  33.  
  34. //***************** LOCAL FUNCTIONS  ******************
  35.  
  36. function findTable(){
  37.   var tableObj="";
  38.   var selArr = dreamweaver.getSelection();
  39.   var selObj = dreamweaver.offsetsToNode(selArr[0],selArr[1]);
  40.  
  41.   while (tableObj=="" && selObj.parentNode){
  42.     if (selObj.nodeType == Node.ELEMENT_NODE && selObj.tagName=="TABLE")
  43.       tableObj=selObj;
  44.     else
  45.       selObj = selObj.parentNode;
  46.   }
  47.   return tableObj;
  48. }
  49.  
  50. function updatePreview(){
  51.   alternateRows(findObject("presetNames").selectedIndex,getPreviewTable());
  52. }
  53.  
  54. function removeTag( theObj, Tag )
  55. {
  56.     var children   = theObj.childNodes;
  57.     var nChildren  = children.length;
  58.  
  59.     for( var i = 0; i < nChildren; i++ )
  60.     {
  61.          var currentChild = children.item(i);
  62.  
  63.          if ( currentChild.hasChildNodes() )
  64.             removeTag( currentChild, Tag );
  65.  
  66.          if ( currentChild.nodeType == Node.ELEMENT_NODE &&
  67.               currentChild.tagName == Tag )
  68.               currentChild.outerHTML = currentChild.innerHTML;
  69.      }
  70. }
  71.  
  72.  
  73. function applyFormatToSelectedTable(){
  74.  
  75.   var selObj,selArr;
  76.   
  77.   //get current selection
  78.   selArr = dreamweaver.getSelection();
  79.   selObj = dreamweaver.offsetsToNode(selArr[0],selArr[1]); 
  80.   alternateRows(findObject("presetNames").selectedIndex,findTable());
  81.   
  82.   //restore original selection
  83.   selArr = dreamweaver.nodeToOffsets(selObj);
  84.   dreamweaver.setSelection(selArr[0],selArr[1]);
  85.   
  86.   savePreferences();
  87. }
  88.  
  89. function isUnrecognizedTable(tableObj) { //checks selected table
  90.   var counter=0;
  91.   var trIter = tableObj.childNodes;
  92.   var trNode=trIter.item(counter);
  93.   var retVal=false;
  94.   while (trNode && !retVal) {
  95.     if (trNode.tagName!="TR" || trNode.childNodes.length==0) 
  96.       retVal=true;
  97.     else trNode=trIter.item(++counter)
  98.   }
  99.   return retVal;
  100. }
  101.  
  102. function hasCaption(theObj){
  103.   if (theObj.childNodes.item(0).tagName && theObj.childNodes.item(0).tagName=="CAPTION")
  104.     return true;
  105.   return false;
  106. }
  107.  
  108. function initializeUI() { //fill presetNames list
  109.   var mainLayer=findObject("mainLayer");
  110.   var presetNamesList,nSize,Names,i;
  111.   var tableObj=findTable();
  112.   var selectedFormat;
  113.  
  114.  if (hasCaption(tableObj))
  115.      mainLayer.innerHTML="<p> </p>" + MSG_CaptionIsPresent;
  116.  else if (isUnrecognizedTable(tableObj))
  117.     mainLayer.innerHTML="<p> </p>" + MSG_IsInvalidTable;
  118.  else {
  119.       //add select list options
  120.       mainLayer.visibility="hidden";
  121.       for (i=0;i<4;i++){  //populate select lists
  122.         findObject("topRowAlign").options[i] = new Option(OPTIONS_Align[i]);
  123.         findObject("topRowTextStyle").options[i]=new Option(OPTIONS_Text_Style[i]);
  124.         findObject("leftColAlign").options[i] = new Option(OPTIONS_Align[i]);
  125.         findObject("leftColTextStyle").options[i] = new Option(OPTIONS_Text_Style[i]);
  126.         findObject("rowLimit").options[i] = new Option(OPTIONS_Row_Limit[i]);
  127.       }
  128.       findObject("rowLimit").options[4] = new Option(OPTIONS_Row_Limit[4]);
  129.  
  130.     presetNamesList = findObject("presetNames");
  131.     Names=tableFormats();  //get list of table format names from tableFormats.js file
  132.     nSize = Names.length;
  133.     for (i=0;i<nSize;i++) { 
  134.       presetNamesList.options[i] = new Option(Names[i].name); //populate form field
  135.     }
  136.     findObject("mainLayer").visibility="visible";
  137.     
  138.     selectedFormat = getFormatPreference();
  139.     setUI(selectedFormat); //choose preferred format
  140.     alternateRows(selectedFormat,getPreviewTable()); //apply this format to preview table
  141.     presetNamesList.selectedIndex=selectedFormat; //select default format
  142.  } 
  143. }
  144.  
  145.  
  146. function alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor){
  147.   var rowLen=trIter.length;
  148.   var cellLen,tdNode,trNode,counter;
  149.   if (rowLimit==0) rowLimit=1000; //repeat first color if rowLimit is equal to 0 
  150.   var counter=2*rowLimit;
  151.   var startRow = (topRowColor)?1:0;
  152.   for (i=startRow;i<rowLen;i++){
  153.     trNode=trIter.item(i);
  154.     rowColor=(counter%(2*rowLimit)<rowLimit)?firstRowColor:secondRowColor;
  155.     if (!useTD) //if bgcolor attribute is attached to tr tags
  156.       trNode.setAttribute("bgcolor",rowColor);
  157.     else { //else attach bgcolor attribute to td tags
  158.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  159.       for (j=0;j<cellLen;j++) 
  160.         tdIter.item(j).setAttribute("bgcolor",rowColor);
  161.     }
  162.     counter++; 
  163.   }
  164. }
  165.  
  166. function formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD){
  167.   var trNode=trIter.item(0), cellLen;
  168.  
  169.   //add bgcolor & align
  170.   if (!useTD) { //if attaching bgcolor & align to first row TR tag
  171.     if (topRowColor)
  172.       trNode.setAttribute("bgcolor",topRowColor);
  173.     if ( !topRowAlign || topRowAlign.toLowerCase() == "none" )
  174.       trNode.removeAttribute("align");
  175.     else
  176.       trNode.setAttribute("align",topRowAlign);
  177.   } else { //else attach bgcolor & align to TD tags in first row
  178.     tdIter=trNode.childNodes; cellLen=tdIter.length;
  179.     for (i=0;i<cellLen;i++){
  180.      tdNode=tdIter.item(i);
  181.      if (topRowColor)
  182.        tdNode.setAttribute("bgcolor",topRowColor);
  183.      if ( !topRowAlign || topRowAlign.toLowerCase()=="none" )
  184.        tdNode.removeAttribute("align");
  185.      else
  186.        tdNode.setAttribute("align",topRowAlign);      
  187.      
  188.     }
  189.   } 
  190.  
  191.   //add text color and text formatting
  192.   tdIter=trNode.childNodes; cellLen=tdIter.length;
  193.    for (i=0;i<cellLen;i++) {
  194.      tdNode=tdIter.item(i);
  195.      //set font color, if specified
  196.      if (topRowTextColor){
  197.        if (!findTag(tdNode,"FONT")){ //if the color attribute cannot be added to a "safe" font tag, add it.
  198.          tdNode.innerHTML="<font color='" + topRowTextColor + "'>" + tdNode.innerHTML + "</font>";
  199.        }
  200.        else  { //font tag already exists, set color attribute on it
  201.          findTag(tdNode,"FONT").setAttribute("color",topRowTextColor);
  202.        }
  203.      }
  204.       //set text style
  205.      if (topRowTextStyle==0){//if text style set to none
  206.        removeTag(tdNode,"B"); //remove bold tags
  207.        removeTag(tdNode,"I"); //and remove italic tags
  208.      } else {
  209.        if (topRowTextStyle==1 || topRowTextStyle==3){ //if bold or bold italic
  210.          if (topRowTextStyle==1) //if bold
  211.            removeTag(tdNode,"I"); //remove all italic tags
  212.          if (!findTag(tdNode,"B")){ //if a B tag is not found around the table cell text
  213.            removeTag(tdNode,"B"); //remove any other B tags
  214.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  215.          }
  216.        }
  217.        if (topRowTextStyle==2 || topRowTextStyle==3){ //if italic or or bold italic
  218.          if (topRowTextStyle==2) //if italic
  219.            removeTag(tdNode,"B"); //remove all bold tags
  220.          if (!findTag(tdNode,"I")){ //if an I tag is not found around the table cell text 
  221.            removeTag(tdNode,"I"); //remove any other I tags
  222.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child an I tag
  223.          }
  224.        }
  225.      }
  226.    } 
  227. }
  228.  
  229. function formatLeftCol(trIter,leftColTextStyle,leftColAlign){
  230.   rowLen=trIter.length;
  231.    for (i=0;i<rowLen;i++){
  232.      trNode=trIter.item(i);
  233.      tdNode=trNode.childNodes.item(0);
  234.      //set text style
  235.      if (leftColTextStyle==0){ //if text style set to none
  236.        removeTag(tdNode,"B"); //remove bold tags
  237.        removeTag(tdNode,"I"); //and remove italic tags
  238.      }
  239.      else {
  240.        if (leftColTextStyle==1 || leftColTextStyle==3){ //if text style set to bold or bold italic
  241.          if (leftColTextStyle==1) //if bold
  242.            removeTag(tdNode,"I"); //remove all italic tags
  243.          if (!findTag(tdNode,"B")){ //if B tag is not found around text
  244.            removeTag(tdNode,"B"); //remove all other B tags
  245.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  246.          }
  247.     
  248.        }
  249.        if (leftColTextStyle==2 || leftColTextStyle==3){ //if text style is set to italic or bold italic
  250.          if (leftColTextStyle==2) //if italic
  251.            removeTag(tdNode,"B"); //remove all bold tags
  252.          if (!findTag(tdNode,"I")){ //if i tag is not found around text
  253.            removeTag(tdNode,"I"); //remove all other i tags
  254.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child tag i tag
  255.          }
  256.        }
  257.      }     
  258.      //set alignment
  259.      if ( !leftColAlign || leftColAlign.toLowerCase()=="none" )
  260.        tdNode.removeAttribute("align");
  261.      else
  262.        tdNode.setAttribute("align",leftColAlign);        
  263.    }
  264. }
  265.  
  266. function setUI(presetIndex){
  267.       var Names = tableFormats();
  268.       var thisFormat = Names[presetIndex];               
  269.       with (thisFormat){
  270.         //error check rowLimit value
  271.         if (rowLimit>4 || rowLimit<0)rowLimit=1;
  272.     
  273.         //align attributes are specified as "left","center","right", and "";
  274.         //following function gets correct selected index for alignment option
  275.         topRowAlign=getIndex(topRowAlign);
  276.         leftColAlign=getIndex(leftColAlign); 
  277.         //select the appropriate options
  278.         findObject("rowLimit").selectedIndex=parseInt(rowLimit);
  279.         findObject("topRowAlign").selectedIndex=topRowAlign;
  280.         findObject("topRowTextStyle").selectedIndex=topRowTextStyle;
  281.         findObject("leftColAlign").selectedIndex=leftColAlign;
  282.         findObject("leftColTextStyle").selectedIndex=leftColTextStyle;
  283.       
  284.         //fill in textfields
  285.         findObject("firstRowColor").value = firstRowColor;
  286.         findObject("secondRowColor").value = secondRowColor;
  287.         findObject("topRowColor").value = topRowColor;
  288.         findObject("topRowTextColor").value = topRowTextColor;
  289.         findObject("borderSize").value = border;
  290.       }
  291.  
  292. }
  293. function alternateRows(presetChoiceIndex,tableObj){
  294.   var tableNode,trIter,trNode,tdIter,tdNode,counter;
  295.   var useTD,topRowColor,selInd,topRowAlign,topRowTextColor;
  296.   var topRowTextStyle,firstRowColor,secondRowColor;
  297.   var rowLimit,borderSize;
  298.  
  299.   tableNode=tableObj;
  300.   trIter=tableNode.childNodes;
  301.   trNode,tdIter,tdNode;
  302.   useTD=findObject('useTD').checked?true:false;
  303.   
  304.   //The rest of the function assigns values to below variables based on user interface.
  305.   //User interface is initially populated with argument values, but the user can change
  306.   //them to dynamically update the preview table.
  307.  
  308.   //set variables for the top row
  309.   topRowColor=findObject('topRowColor').value;
  310.   selInd = findObject('topRowAlign').selectedIndex;
  311.   topRowAlign=findObject('topRowAlign').options[selInd].value;
  312.   topRowTextColor=findObject('topRowTextColor').value;
  313.   topRowTextStyle=findObject('topRowTextStyle').selectedIndex;
  314.   
  315.   //set variables for the left col
  316.   selInd = findObject('leftColAlign').selectedIndex;
  317.   leftColAlign=findObject('leftColAlign').options[selInd].value;
  318.   leftColTextStyle=findObject('leftColTextStyle').selectedIndex;
  319.   
  320.   //set variables for the row Colors
  321.   firstRowColor=findObject('firstRowColor').value;
  322.   secondRowColor=findObject('secondRowColor').value;
  323.   rowLimit=findObject("rowLimit").selectedIndex;
  324.   
  325.   //set border size
  326.   borderSize=findObject("borderSize").value;
  327.  
  328.   //Now, use these values to format the table...
  329.   
  330.   //set table border
  331.   tableNode.setAttribute("border",borderSize); 
  332.  
  333.   //alternate row Colors  
  334.   alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor);
  335.  
  336.   //add left col formatting:text style & alignment
  337.   formatLeftCol(trIter,leftColTextStyle,leftColAlign);
  338.  
  339.   //add top row formatting:text style,alignent,row color, & text color
  340.   formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD);
  341.   
  342.   //set border
  343.   tableNode.setAttribute("border",borderSize);
  344.  
  345.   //if there is a background color, remove it.
  346.   if (tableNode.getAttribute("bgColor")) {
  347.     tableNode.removeAttribute("bgColor");
  348.   }
  349. }
  350.  
  351. function updatePreviewTable(){
  352.   var presetIndex = findObject("presetNames").selectedIndex;
  353.   var previewTable = getPreviewTable();
  354.   var mainLayer = findObject("mainLayer");
  355.   
  356.   setUI(presetIndex);
  357.   alternateRows(findObject("presetNames").selectedIndex,previewTable);//format table
  358. }
  359.  
  360. function getPreviewTable(){
  361.    //returns preview Table object
  362.    return document.getElementsByTagName("TABLE").item(1);
  363. }
  364.  
  365.  
  366. function getIndex(align){
  367.   //returns align index of UI that matches text of align argument i.e: "center"
  368.   //Note: I didn't use constants here because it is more intuitive to define
  369.   //the alignment attribute following the html syntax of align="attribute"
  370.     switch (align){
  371.      case "left": align=1; break;
  372.      case "center": align=2; break;
  373.      case "right": align=3; break;
  374.      default: align=0; break;
  375.    }
  376.  return align;
  377. }
  378.  
  379. //searches the child nodes of tableCellObj -
  380. //returns the innermost object of tagName that surrounds all of the text in
  381. //the cell. returns empty string if not found.
  382. function findTag(obj, tag) {
  383.   var retVal="";
  384.   while (obj.childNodes && obj.childNodes.length == 1) {
  385.     obj = obj.childNodes.item(0);
  386.    if (obj.nodeType == Node.ELEMENT_NODE && obj.tagName == tag)
  387.      retVal=obj;
  388.   }
  389.   return retVal;
  390.  
  391. }
  392.  
  393.  
  394. function getFormatPreference() {
  395.   var metaFile, savedVal, curVal = DEFAULT_tableStyle;
  396.   if (typeof MMNotes != 'undefined') { // Check for MMNotes extension.
  397.    metaFile = MMNotes.open(document.URL, false);
  398.    if (metaFile) {
  399.      // Form specific settings.
  400.      savedVal = MMNotes.get(metaFile, 'MM_pref_FormatTable');
  401.      MMNotes.close(metaFile);
  402.      if (curVal == parseInt(curVal).toString()) {
  403.        curVal = savedVal;
  404.      }
  405.    }
  406.   }
  407.   return parseInt(curVal);
  408. }
  409.  
  410. function savePreferences() {
  411.   if (typeof MMNotes == 'undefined') {return;} // Check for MMNotes extension.
  412.   var metaFile, curVal;
  413.   metaFile = MMNotes.open(document.URL, true);
  414.   if (metaFile) {
  415.     curVal = MMNotes.set(metaFile, 'MM_pref_FormatTable', findObject("presetNames").selectedIndex);
  416.     MMNotes.close(metaFile);
  417.   }
  418. }
  419.  
  420. //**************** GENERIC FUNCTIONS ****************
  421.  
  422. function findObject(objName,  parentObj) {
  423.   var i,tempObj="",found=false,curObj = "";
  424.   var NS = (navigator.appName.indexOf("Netscape") != -1);
  425.   if (!NS && document.all) curObj = document.all[objName]; //IE4
  426.   else {
  427.     parentObj = (parentObj != null)? parentObj.document : document;
  428.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  429.     else { //if in form
  430.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  431.         if (parentObj.forms[i][objName]) {
  432.           curObj = parentObj.forms[i][objName];
  433.           found = true; break;
  434.       } }
  435.       if (!found && NS && parentObj.layers && parentObj.layers.length > 0) {
  436.         parentObj = parentObj.layers;
  437.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  438.           tempObj = findObject(objName,parentObj[i]); //recurse
  439.           if (tempObj) { curObj = tempObj; break;} //if found, done
  440.   } } } }
  441.   return curObj;
  442. }
  443.  
  444.  
  445.  
  446.